#include "RingQueue.hpp"
#include "Task.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <ctime>

void *Consumer(void *args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        Task t;
        //1. 消费
        rq->Pop(&t);
        //2.处理数据
        t();
         std::cout << "Consumer-> " << t.result() << std::endl;
    }
}
void *Producter(void *args)
{
    RingQueue<Task>*rq=static_cast<RingQueue<Task>*>(args);
    while (true)
    {
        sleep(1);
        RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
        //1.构造数据
        int x = rand() % 10 + 1; //[1, 10]
        usleep(x*1000);
        int y = rand() % 10 + 1;
        Task t(x, y);
        //2.生产
        rq->Push(t);
        std::cout << "Productor -> " << t.debug() << std::endl;
    }
    
}


int main()
{
    RingQueue<Task>*rq=new RingQueue<Task>(1);
    //单单
    pthread_t c,p;
    pthread_create(&c,nullptr,Consumer,rq);
    pthread_create(&p,nullptr,Producter,rq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);
}